﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Helper;

namespace StudentStatistics
{
    /// <summary>
    /// ResultsMainControl.xaml 的交互逻辑
    /// </summary>
    public partial class ResultsMainControl : UserControl
    {

        public static ResultsMainControl Instacne
        {
            get;
            private set;
        }

        /// <summary>
        /// 主浏览界面 成员正确率
        /// </summary>
        private StudentAchievementControl StudentAchievementControlUI = new StudentAchievementControl();
        private StudentAnswerControl StudentAnswerControlUI = new StudentAnswerControl();
        private StudentCompletionStatisticsControl StudentCompletionStatisticsControlUI = new StudentCompletionStatisticsControl();
        private StudentAccuracyControl StudentAccuracyControlUI = new StudentAccuracyControl();

        /// <summary>
        /// 各题正确率
        /// </summary>
        private QuestionRightRateControl rightcontrol = new QuestionRightRateControl();

        /// <summary>
        /// 时间
        /// </summary>
        private EveryQuestionTimeControl timecontrol = new EveryQuestionTimeControl();

        private TittleButton OldTittleButton = null;

        public ResultsMainControl()
        {
            Instacne = this;
            InitializeComponent();
            this.Loaded += ResultsMainControl_Loaded;
        }

        public void ResultsMainControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetMainUI(true);

            WinplayBtn.SetPress();
        }

        /// <summary>
        /// 点击返回键出现界面
        /// </summary>
        public void SetMainUI(bool forceview = false)
        {
            if (forceview)
            {
                AchievementButton.SetPress();
            }
            SetTittleButtonView(AchievementButton);
            this.viewcontrol.Children.Clear();
            this.viewcontrol.Children.Add(StudentAchievementControlUI);
        }

        public void SetTimeUI()
        {
            SetTittleButtonView(TimeButton);

            this.viewcontrol.Children.Clear();
            timecontrol.orderItem = StudentAchievementControl.Instance.orderItem;
            this.viewcontrol.Children.Add(timecontrol);
        }

        /// <summary>
        /// 成员正确率
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Achievement_Click(object sender, RoutedEventArgs e)
        {
            SetTittleButtonView(sender);
            this.viewcontrol.Children.Clear();
            this.viewcontrol.Children.Add(StudentAchievementControlUI);
        }

        /// <summary>
        /// 各题正确率
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Answer_Click(object sender, RoutedEventArgs e)
        {
            SetTittleButtonView(sender);
            this.viewcontrol.Children.Clear();
            this.viewcontrol.Children.Add(rightcontrol);
        }

        /// <summary>
        /// 平均正确率
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Completion_Click(object sender, RoutedEventArgs e)
        {
            SetTittleButtonView(sender);
            this.viewcontrol.Children.Clear();
            this.viewcontrol.Children.Add(StudentCompletionStatisticsControlUI);
        }


        /// <summary>
        /// 成员世间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Time_Click(object sender, RoutedEventArgs e)
        {
            SetTittleButtonView(sender);
            this.viewcontrol.Children.Clear();
            timecontrol.orderItem = StudentAchievementControl.Instance.orderItem;
            this.viewcontrol.Children.Add(timecontrol);
        }

        /// <summary>
        /// 平均用时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Accuracy_Click(object sender, RoutedEventArgs e)
        {
            this.viewcontrol.Children.Clear();
            this.viewcontrol.Children.Add(StudentAccuracyControlUI);
            SetTittleButtonView(sender);
        }

        public void AddViewControl(UIElement element)
        {
            this.viewcontrol.Children.Clear();
            this.viewcontrol.Children.Add(element);
        }

        protected void SetTittleButtonView(object sender)
        {
            var uielement = sender as UIElement;
            if (uielement != null)
            {
                var tittlebutton = uielement.FindVisualParent<TittleButton>();
                if (tittlebutton != null)
                {
                    if (OldTittleButton != null)
                    {
                        OldTittleButton.SetUnPress();
                    }
                    tittlebutton.SetPress();
                    OldTittleButton = tittlebutton;
                }
            }
        }
        /// <summary>
        /// 推出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PressButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.Close();
        }

        private void WinplayBtn_Click(object sender, RoutedEventArgs e)
        {
            SetTittleButtonView(sender);
        }


    }
}
